package com.demo.myapplication.utils;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.content.res.Resources;
import android.graphics.drawable.Drawable;
import android.os.Build;
import android.preference.PreferenceManager;
import android.provider.Settings;
import android.util.DisplayMetrics;
import android.util.Log;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;

import com.demo.myapplication.R;
import com.demo.myapplication.service.FloatingService;

public class CommonUtils {
  public static final String HTTPS_PRE = "https://";
  private static String Tag = CommonUtils.class.getSimpleName();

  public static final String USER_BRIGHTNESS = "user_brightness";
  public static final String USER_BRIGHTNESS_MODE = "user_brightness_mode";  //true follow system,false else
  public static int NON_INT_DATA = -1;
  public static String IS_NIGHT_MODE = "is_night_mode";

  public static String map_icon_key = "icon";
  public static String map_text_key = "text";

  public static int[] icon = {R.drawable.ic_menu_camera, R.drawable.ic_menu_gallery,
      R.drawable.ic_menu_manage, R.drawable.ic_menu_send, R.drawable.ic_menu_share,
      R.drawable.ic_menu_slideshow, R.drawable.ic_remove, R.drawable.ic_small_icon};

  public static int[] iconName = {R.string.screen_brightness, R.string.https_request, R.string.text_notification, R.string.canvas
      , R.string.file_read, R.string.actionmode, R.string.action_remove, R.string.action_down};

  public static void changeTheme(boolean isNight, Context context) {
    if (isNight) {
      context.setTheme(R.style.AppTheme_Dark_NoActionBar);
    } else {
      context.setTheme(R.style.AppTheme_Light_NoActionBar);
    }
  }

  public static void removeShade(Context context) {
    Intent service = new Intent(context, FloatingService.class);
    context.stopService(service);
  }

  public static void addShade(Context context) {
    Intent service = new Intent(context, FloatingService.class);
    context.startService(service);
  }


  public static int getCurrentLight(Context context) throws Settings.SettingNotFoundException {
    boolean savedMode = PreferenceManager.getDefaultSharedPreferences(context).getBoolean(CommonUtils.USER_BRIGHTNESS_MODE, true);
    if (savedMode) {
      //follow system
      Log.d(Tag, "system brightness" + Settings.System.getInt(context.getContentResolver(), Settings.System.SCREEN_BRIGHTNESS));
      return NON_INT_DATA;
    } else {
      return PreferenceManager.getDefaultSharedPreferences(context).getInt(CommonUtils.USER_BRIGHTNESS, NON_INT_DATA);
    }
  }

  public static void changeAppBrightness(Context context, int brightness) {
    Window window = ((Activity) context).getWindow();
    WindowManager.LayoutParams lp = window.getAttributes();
    if (brightness == -1) {
      lp.screenBrightness = lp.buttonBrightness = WindowManager.LayoutParams.BRIGHTNESS_OVERRIDE_NONE;
    } else if (brightness == 255) {
      lp.screenBrightness = lp.buttonBrightness = WindowManager.LayoutParams.BRIGHTNESS_OVERRIDE_FULL;
    } else {
      lp.screenBrightness = lp.buttonBrightness = (brightness <= 0 ? 1 : brightness) / 255f;
    }
    Log.d(Tag, "get brightness= " + brightness + ",set brightness to" + lp.screenBrightness);
    window.setAttributes(lp);
  }

  public static void setNight(Context context) {
    boolean isNight = PreferenceManager.getDefaultSharedPreferences(context).getBoolean(CommonUtils.IS_NIGHT_MODE, false);
    CommonUtils.changeTheme(isNight, context);
  }

  public static void setBackgroundDrawable(View view, Drawable drawable) {
    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN) {
      view.setBackground(drawable);
    } else {
      //noinspection deprecation
      view.setBackgroundDrawable(drawable);
    }
  }

  public static float getDensity() {
    return Resources.getSystem().getDisplayMetrics().density;
  }

  public static int getScreenWidth() {
    return Resources.getSystem().getDisplayMetrics().widthPixels;
  }
  public static int getScreenHeight() {
    return Resources.getSystem().getDisplayMetrics().heightPixels;
  }

  public enum LAUNCH_MODE{
    SINGLE_INSTANCE,
    SINGLE_TASK,
    SINGLE_TOP
  }
}
